#include "errorsbuf.h"
#ifdef __GNUC__
#include <stdexcept>
#endif // __GNUC__

using std::runtime_error;
std::list<ErrorInfo> CompilationErrors::errorsList;

void CompilationErrors::Add(int i_lineNum, std::string i_fileName, const char* i_fullErrDesc)
{
   ErrorInfo* errInfo = new ErrorInfo();

   errInfo->lineNum = i_lineNum;
   errInfo->fileName = i_fileName;
   if (i_fullErrDesc[0] != '\0')
   {
       //trim full error description
       std::string strMessage = i_fullErrDesc;
       strMessage.erase(0, strMessage.find_first_not_of(' '));
       strMessage.erase(0, strMessage.find_first_not_of('\t'));
       if (strMessage[strMessage.length() - 1] == '\n')
           strMessage[strMessage.length() - 1] = '\0';

       COPY_CZ(errInfo->fullErrDesc, strMessage.c_str());
   }

   errorsList.push_back(*errInfo);
}

void CompilationErrors::Add(std::string i_errorString, int i_lineNum, std::string i_fileName, const char* i_fullErrDesc)
{
    ErrorInfo* errInfo = new ErrorInfo();

    errInfo->lineNum = i_lineNum;
    errInfo->fileName = i_fileName;
    COPY_CZ(errInfo->fullErrDesc, i_fullErrDesc)

    errorsList.push_back(*errInfo);

    runtime_error(i_errorString.c_str());
}

std::string CompilationErrors::GetErrorFullDesc()
{
	for (std::list<ErrorInfo>::iterator it = errorsList.begin(); it != errorsList.end(); it++)
	{
		return  (*it).fullErrDesc;
	}

	return "";
}

int CompilationErrors::GetErrorFileLine()
{
	for (std::list<ErrorInfo>::iterator it = errorsList.begin(); it != errorsList.end(); it++)
	{
		return  (*it).lineNum;
	}

	return -1;
}

std::string CompilationErrors::GetErrorFileName()
{
	for (std::list<ErrorInfo>::iterator it = errorsList.begin(); it != errorsList.end(); it++)
	{
		return  (*it).fileName;
	}

	return "<without information>";
}

void CompilationErrors::Clear()
{
   errorsList.clear();
}
